package org.ranch.web;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.ranch.model.Concept;
import org.ranch.model.ConceptCategory;
import org.ranch.model.Farm;
import org.ranch.model.Permission;
import org.ranch.model.Person;
import org.ranch.model.Role;
import org.ranch.web.propertyeditors.ConceptCategoryPropertyEditor;
import org.ranch.web.propertyeditors.ConceptPropertyEditor;
import org.ranch.web.propertyeditors.DatePropertyEditor;
import org.ranch.web.propertyeditors.FarmPropertyEditor;
import org.ranch.web.propertyeditors.PermissionPropertyEditor;
import org.ranch.web.propertyeditors.PersonPropertyEditor;
import org.ranch.web.propertyeditors.RolePropertyEditor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;

/**
 * this is the Global Data binding initializers that attaches different property
 * editors for different entity classes
 * 
 * 
 */
public class RanchBindingInitializer implements WebBindingInitializer {

    @Autowired
    private ConceptPropertyEditor conceptPropertyEditor;

    @Autowired
    private RolePropertyEditor rolePropertyEditor;

    @Autowired
    private PermissionPropertyEditor permissionPropertyEditor;

    @Autowired
    private ConceptCategoryPropertyEditor conceptCategoryPropertyEditor;
    
    @Autowired
    private PersonPropertyEditor personPropertyEditor;

    @Autowired
    private FarmPropertyEditor farmPropertyEditor;

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.springframework.web.bind.support.WebBindingInitializer#initBinder
     * (org.springframework.web.bind.WebDataBinder,
     * org.springframework.web.context.request.WebRequest)
     */
    @Override
    public void initBinder(WebDataBinder binder, WebRequest request) {
	binder.registerCustomEditor(Concept.class, conceptPropertyEditor);
	binder.registerCustomEditor(Date.class, new DatePropertyEditor(new SimpleDateFormat(
		"dd/MM/yyyy")));
	binder.registerCustomEditor(Role.class, rolePropertyEditor);
	binder.registerCustomEditor(Permission.class, permissionPropertyEditor);
	binder.registerCustomEditor(ConceptCategory.class, conceptCategoryPropertyEditor);
	binder.registerCustomEditor(Person.class, personPropertyEditor);
	binder.registerCustomEditor(Farm.class, farmPropertyEditor);
    }
}
